package com.six.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
import java.util.Date;

// util/JwtUtil.java
@Component
public class JwtUtil {

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.expire}")
    private long expireSeconds;

    public String generateToken(Long userId, String username, Integer roleId, Long storeId,String storeName) {
        Date now = new Date();
        return Jwts.builder()
                .setSubject(userId.toString())
                .claim("username", username)
                .claim("role", roleId)
                .claim("storeId", storeId)
                .claim("storeName", storeName)
                .setIssuedAt(now)
                .setExpiration(new Date(now.getTime() + expireSeconds * 1000))
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    public Claims parseToken(String token) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }
    public long getExpire() {
        return expireSeconds;
    }
}
